package com.example.siyun.controller.permissionscontroller;

import com.example.siyun.pojo.BackofficeTable;
import com.example.siyun.pojo.EnumPojo;
import com.example.siyun.service.permissionsservice.BackofficeService;
import com.example.siyun.util.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Auther: 全
 * @Date: 2022/9/26 09:33
 * @Description:权限管理模块后台管理人员列表
 */
@Slf4j
@RestController
public class BackofficeController {
    @Autowired
    private BackofficeService backofficeService;
    /**
     * 获取后台管理人员列表的数据
     * @return 返回台管理人员列表的数据
     */
    @RequestMapping("/intermediate/getbackofficetree")
    public Map<String, Object> getTree() {
        log.info("获取后台管理人员列表的数据");
        Map<String, Object> map = new HashMap<>();
        map.put("menulists", backofficeService.getBackOfficeTree());
        log.info("获取后台管理人员列表的数据成功");
        return map;
    }
    /**
     * 获取后台管理人员列表的数据
     * @return 返回台管理人员列表的数据
     */
    @RequestMapping("/advanced/getbackofficetable")
    public PageResult getAll(BackofficeTable backofficetable, int page, int limit) {
        log.info("获取后台管理人员列表的数据");
        PageHelper.startPage(page, limit);
        List<BackofficeTable> list = backofficeService.getBackOfficeTable(backofficetable, EnumPojo.VALUE_DEL_0);
        PageInfo<BackofficeTable> pageInfo = new PageInfo<>(list);
        PageResult pageResult=new PageResult();
        pageResult.setState(0);
        pageResult.setMessage("成功");
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setData(pageInfo.getList());
        log.info("获取后台管理人员列表的数据成功");
        return pageResult;
    }
    /**
     * 获取后台管理人员列表的数据通过id
     * @return id要获取的后台管理人员的id
     */
    @RequestMapping("/advanced/getbackofficetablebyid")
    public PageResult getBackOfficeTableById(int id){
        log.info("获取后台管理人员列表的数据通过id");
        BackofficeTable backofficetable=backofficeService.getBackOfficeTableById(id);
        PageResult pageResult=new PageResult();
//        pageResult.setStatus(200);
        pageResult.setData(backofficetable);
        log.info("获取后台管理人员列表的数据通过id成功");
        return pageResult;
    }
    @RequestMapping("/advanced/setpermission")
    public PageResult setPermission(BackofficeTable backofficetable) {
        backofficeService.setPermissions(backofficetable);
        Map<String, Object> map = new HashMap<>();
        PageResult pageResult=new PageResult();
        pageResult.setMessage("成功");
//        pageResult.setStatus(200);
        return pageResult;
    }
    @RequestMapping("/advanced/setpermissionstable")
    public PageResult setPermissionsTable(BackofficeTable backofficetable) {
        backofficeService.setPermissionsTable(backofficetable);
        PageResult pageResult=new PageResult();
        pageResult.setMessage("成功");
//        pageResult.setStatus(200);
        return pageResult;
    }
    @RequestMapping("/advanced/updatestate")
    public PageResult updateState(int id,int state) {
        if(state==0){
            backofficeService.updateState(id,state);
        }else if(state==1){
            backofficeService.updateState(id,state);
            backofficeService.resetAccount(id,EnumPojo.STATE_0);
        }
        PageResult pageResult=new PageResult();
        pageResult.setMessage("成功");
//        pageResult.setStatus(200);
        return pageResult;
    }
}
